package com.sz.zhiling.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class TokenUtils {
    @Value("${appID}")
    private String appID;
    @Value("${appsecret}")
    private String appsecret;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 public String getToken(){
     String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appID+"&secret="+appsecret;
     String token=stringRedisTemplate.opsForValue().get("access_token");
     if (token==null){
         token= com.sz.zhiling.common.HttpClientUtils.getMethod(url);
         JSONObject json= JSON.parseObject(token);
         String code =json.get("access_token").toString();
         stringRedisTemplate.opsForValue().set("access_token",code);
     }
     return token;
 }
}
